/**
 * 
 */
package info.noahcampbell.meter.management;

import info.noahcampbell.meter.management.plugin.RrdManager;
import info.noahcampbell.meter.messenger.accumulator.Accumulator;

import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.Preferences;

import javax.management.InstanceNotFoundException;
import javax.management.IntrospectionException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.servlet.GenericServlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.jrobin.core.RrdException;

/**
 * @author Noah Campbell
 * @version 1.0
 */
public class RrdManagerServlet extends GenericServlet {

    private RrdManager rrdManager;
    private MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    private final static Logger logger = Logger.getLogger("rrdmanagerservlet");
    /**
     * @see javax.servlet.GenericServlet#init()
     */
    @Override
    public void init() throws ServletException {
        super.init();
        
        ServletConfig config = this.getServletConfig();
        String baseRepo = config.getInitParameter("repository");
        
        
        File store = new File(baseRepo, "store");
        if(!store.exists()) {
            store.mkdirs();
        }
        
        try {
            rrdManager = RrdManager.instance();
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
            throw new ServletException(e1);
        }
        
        Preferences preferences = Preferences.systemNodeForPackage(RrdManagerServlet.class);
        String monitoredObjectNames = preferences.get("monitoredObjectNames", "java.lang:type=Threading;java.lang:type=Memory");
        for(String name : monitoredObjectNames.split("; ?")) {
            logger.log(Level.FINER, "initialize.objectname", name);
            try {
                ObjectName objectName = new ObjectName(name);
                Accumulator accum = rrdManager.create(objectName);
               
                /**
                 * Schedule the accumulator.
                 */
                executor.scheduleAtFixedRate(accum, 0, 300, TimeUnit.SECONDS);
                
            } catch (MalformedObjectNameException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (NullPointerException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InstanceNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (RrdException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IntrospectionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ReflectionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
        }
    }
    
    private ScheduledExecutorService executor = Executors.newScheduledThreadPool(10);
    /**
     * @see javax.servlet.GenericServlet#service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
     */
    @Override
    public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
        // TODO Auto-generated method stub
        
    }
    /**
     * @see javax.servlet.GenericServlet#destroy()
     */
    @Override
    public void destroy() {
        super.destroy();
        RrdManager.release();
    }
    

}
